package com.dormitory.controller;


import com.dormitory.po.Class;
import com.dormitory.po.Dormitory;
import com.dormitory.po.PageInfo;
import com.dormitory.po.Student;
import com.dormitory.service.ClassService;
import com.dormitory.service.DormitoryService;
import com.dormitory.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户控制器类
 */
@Controller
public class StudentController {
    // 依赖注入
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassService classService;
    @Autowired
    private DormitoryService dormitoryService;

    @RequestMapping("/getOthersToAddStudent")
    public String getOthersToAddStudent(Model model) {
        List<Class> classList = classService.getAll();
        model.addAttribute("classList", classList);
        List<Dormitory> dormitoryList = dormitoryService.getAll();
        model.addAttribute("dormitoryList", dormitoryList);
        return "student_add";

    }

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findStudent")
    public String findStudent(String s_name, Integer s_studentid, Integer s_classid, String s_classname,
                              Integer pageIndex, Integer pageSize, Model model) {

        PageInfo<Student> pi = studentService.findPageInfo(s_name, s_studentid, s_classid,
                s_classname, pageIndex, pageSize);
        model.addAttribute("pi", pi);
        model.addAttribute("s_name", s_name);
        return "student_list";
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value = "/exportstudentlist", method = RequestMethod.POST)
    @ResponseBody
    public List<Student> exportStudent() {
        List<Student> studentList = studentService.getAll();
        return studentList;
    }

    /**
     * 删除学生信息
     */
    @RequestMapping("/deleteStudent")
    @ResponseBody
    public String deleteStudent(Integer s_id) {
        int s = studentService.deleteStudent(s_id);
        return "student_list";

    }

    /**
     * 添加学生信息
     */
    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
//    @ResponseBody
    public String addStudent(Student student) {
        student.setC_id(1);
        student.setD_id(1);
        studentService.addStudent(student);
        return "redirect:/findStudent";
    }

    /**
     * 修改学生信息
     */
    @RequestMapping(value = "/updateStudent", method = RequestMethod.POST)
    public String updateStudent(Student student) {
        int s = studentService.updateStudent(student);
        return "redirect:/findStudent";
    }


    @RequestMapping("/findStudentById")
    public String findStudentById(Integer s_id, HttpSession session, Model model) {

        Student s = studentService.findStudentById(s_id);
        session.setAttribute("s", s);
        List<Class> classList = classService.getAll();
        model.addAttribute("classList", classList);
        List<Dormitory> dormitoryList = dormitoryService.getAll();
        model.addAttribute("dormitoryList", dormitoryList);
        return "student_edit";
    }
}
